package net.a6polo.a6poloweibo;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.google.gson.Gson;
import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.StatusesAPI;
import com.weibo.sdk.android.net.RequestListener;

import net.a6polo.a6poloweibo.WeiboDetailsActivity.WeiboShowRequestListener;
import net.a6polo.a6poloweibo.sina.domain.WeiboItem;
import net.a6polo.a6poloweibo.util.AsynImageLoader;
import net.a6polo.a6poloweibo.util.WeiboUtil;
import net.a6polo.a6poloweibo.view.MyProgressDialog;
import net.a6polo.service.Service;
import net.a6polo.service.SnsType;
import net.a6polo.service.domain.Account;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class WeiboDetailTab extends Fragment {

	private WeiboItem weibo;

	private Handler handler = null;

	private TextView userNameTextView;

	private ImageView userIcon;

	private ImageView contentPic;

	private ImageView vUser;

	private TextView content;

	private TextView sinaWeiboDate;

	private static String weiboId;

	private Exception exception;

	private Dialog progressDialog;
	
	private View view;
	
	private Account sinaAccount;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Service service = new Service(getActivity());
		sinaAccount = service.getLastAccountByType(SnsType.SINA_WEIBO);
		handler = new Handler();
		progressDialog = new MyProgressDialog(getActivity(), R.style.CustomProgressDialog);
		weiboId = getArguments().getString("weiboId");
		refresh();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.activity_weibo_details, container, false);

		userNameTextView = (TextView) view.findViewById(R.id.sinaWeiboUserName);
		userIcon = (ImageView) view.findViewById(R.id.weiDetailsUserHead);
		vUser = (ImageView) view.findViewById(R.id.weiboDetailsUserV);
		content = (TextView) view.findViewById(R.id.sinaWeiboText);
		sinaWeiboDate = (TextView) view.findViewById(R.id.sinaWeiboDate);
		contentPic = (ImageView) view.findViewById(R.id.weiboDetailsContentPic);

		return view;
	}

	private void refresh() {
		Oauth2AccessToken token = new Oauth2AccessToken(sinaAccount.getAccessToken(), sinaAccount.getExpiresIn());
		StatusesAPI api = new StatusesAPI(token);
		progressDialog.show();
		api.show(Long.parseLong(weiboId), new WeiboShowRequestListener());
	}

	class WeiboShowRequestListener implements RequestListener {

		@Override
		public void onComplete(String json) {
			progressDialog.dismiss();
			Gson gson = new Gson();
			weibo = gson.fromJson(json, WeiboItem.class);

			new Thread() {
				public void run() {
					handler.post(runnableUi);
				}
			}.start();
		}

		@Override
		public void onError(WeiboException e) {
			exception = e;
			getActivity().runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(getActivity().getApplicationContext(), " error : " + exception.getMessage(), Toast.LENGTH_LONG)
							.show();
				}
			});
		}

		@Override
		public void onIOException(IOException e) {
			exception = e;
			getActivity().runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(getActivity().getApplicationContext(), " error : " + exception.getMessage(), Toast.LENGTH_LONG)
							.show();
				}
			});
		}

	}

	Runnable runnableUi = new Runnable() {
		@Override
		public void run() {
			userNameTextView.setText(weibo.getUser().getName());

			AsynImageLoader loader = new AsynImageLoader();
			Bitmap userHead = loader.loadDrawableFromNet(userIcon, weibo.getUser().getProfile_image_url());
			userIcon.setImageBitmap(userHead);

			content.setText(weibo.getText());

			Date datetime = new Date(weibo.getCreated_at());
			SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
			String str = sdf.format(datetime);
			sinaWeiboDate.setText(str);

			/**
			 * ��v��֤
			 */
			if (weibo.getUser().isVerified()) {
				vUser.setVisibility(ImageView.VISIBLE);
			}

			if (null != weibo.getBmiddle_pic()) {
				contentPic.setVisibility(ImageView.VISIBLE);
				Bitmap contentPicMap = loader.loadDrawableFromNet(contentPic, weibo.getBmiddle_pic());
				contentPic.setImageBitmap(contentPicMap);
			}

			/**
			 * ����ת����΢��������ʾת��������
			 */
			if (null != weibo.getRetweeted_status()) {
				LinearLayout item_sublayout = (LinearLayout) view.findViewById(R.id.lyt_wb_item_sublayout);
				item_sublayout.setVisibility(LinearLayout.VISIBLE);
				TextView subcontent = (TextView) view.findViewById(R.id.txt_wb_item_subcontent);
				subcontent.setText(weibo.getRetweeted_status().getText());

				if (null != weibo.getRetweeted_status().getThumbnail_pic()) {
					ImageView sub_content_Pic = (ImageView) view.findViewById(R.id.img_wb_item_content_subpic);
					sub_content_Pic.setVisibility(ImageView.VISIBLE);
					Bitmap contentPic = loader.loadDrawableFromNet(sub_content_Pic, weibo.getRetweeted_status()
							.getThumbnail_pic());
					sub_content_Pic.setImageBitmap(contentPic);

				}
			}

			/**
			 * ������Դ
			 */
			TextView from = (TextView) view.findViewById(R.id.txt_wb_item_from);
			from.setText(Html.fromHtml(weibo.getSource()));
			from.setMovementMethod(LinkMovementMethod.getInstance());

			/**
			 * ����ת�����������
			 */
			TextView redirect = (TextView) view.findViewById(R.id.txt_wb_item_redirect);
			TextView comment = (TextView) view.findViewById(R.id.txt_wb_item_comment);
			redirect.setText(weibo.getReposts_count() + "");
			comment.setText(weibo.getComments_count() + "");
		}
	};
}
